package com.meeting.meetingroom_api.repository;

import com.meeting.meetingroom_api.entity.Booking;
import com.meeting.meetingroom_api.entity.Cancellation;
import com.meeting.meetingroom_api.enums.CancellationStatus;
import com.meeting.meetingroom_api.enums.CustomerConfirmationStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface CancellationRepository extends JpaRepository<Cancellation, Integer>, JpaSpecificationExecutor<Cancellation> {
    Optional<Cancellation> findByBooking(Booking booking);
    Optional<Cancellation> findByBookingBookingId(Integer bookingId);
    
    // 查询所有待处理的取消申请
    Page<Cancellation> findByStatus(CancellationStatus status, Pageable pageable);
    
    // 查询指定客户的待处理取消申请
    Page<Cancellation> findByCustomer_UserId(Integer customerId, Pageable pageable);
    
    // 查询指定客户的待处理取消申请
    Page<Cancellation> findByCustomer_UserIdAndStatus(Integer customerId, CancellationStatus status, Pageable pageable);

    // 查询指定客户的待确认取消申请
    Page<Cancellation> findByCustomer_UserIdAndCustomerConfirmation(Integer customerId, CustomerConfirmationStatus customerConfirmation, Pageable pageable);
    

    // 查询所有待客户确认的取消申请
    Page<Cancellation> findByCustomerConfirmation(CustomerConfirmationStatus customerConfirmation, Pageable pageable);
    // 查询客户ID等于指定值、customerConfirmation为pending，且cancellationStatus不为pending_review的记录
    Page<Cancellation> findByCustomer_UserIdAndCustomerConfirmationAndStatusNot(
            Integer customerId,
            CustomerConfirmationStatus customerConfirmation,
            CancellationStatus exclusionStatus,
            Pageable pageable
    );
    // 查询客户ID等于指定值、customerConfirmation为pending，且cancellationStatus是refunded或其他状态的记录
    Page<Cancellation> findByCustomer_UserIdAndCustomerConfirmationAndStatus(
            Integer customerId,
            CustomerConfirmationStatus customerConfirmation,
            CancellationStatus status,
            Pageable pageable
    );
}